<template>
  <formItem :item="item">
    <a-input-number
      allowClear
      style="width:100%;"
      v-model="item['value']"
      :precision="getSingleInputNumberAttr(item.rules)[0]"
      :max="getSingleInputNumberAttr(item.rules)[1]"
      :min="getSingleInputNumberAttr(item.rules)[2]"
      placeholder="请输入"
      :disabled="disabled"
    />
  </formItem>
</template>

<script>
export default {
  components: {
    formItem: () => import('./formItem.vue')
  },

  props: {
    disabled: {
      type: Boolean
    },

    item: {
      type: Object,
      default() {
        return null
      }
    }
  },

  computed: {
    // 数字类型
    getSingleInputNumberAttr() {
      return rules => {
        let newArr = JSON.parse(JSON.stringify(rules.rule))
        if (!Array.isArray(newArr)) {
          newArr = [newArr]
        }

        let arr = [0, Infinity, -Infinity] //  0.精度 1.最大值 2.最小值

        newArr.forEach(el => {
          let item = el['@attributes']

          switch (item.type) {
            case 'precision':
              arr[0] = parseInt(item.value)
              break

            case 'exclusiveMaximum':
              arr[1] = parseInt(item.value)
              break

            case 'exclusiveMinimum':
              arr[2] = parseInt(item.value)
              break
          }
        })

        return arr
      }
    }
  }
}
</script>
